package model.market;

import java.util.HashMap;
import java.util.Iterator;
import java.util.Map;

import model.trader.Trader;

/**
 * daily market information
 * @author Administrator
 *
 */
//AMLOW: refactor/integrate this with market information? Comment more clearly the difference in intention?
public class DayMarketInfo {

	private double cash;
	
	private int[] totalNetQuantities;
	private double[] marketPrices;
	private double[] annualisedDividend;
	private long[] volumes;
	private int tradersBlown;
	private int tradersRetired;
	
	private double[] cashflowPerShare;
	
	double totalWorth = 0;
	Map<Class, Position> classPositions = new HashMap<Class, Position>();
	
	public DayMarketInfo(int assetCount){
		marketPrices =new double[assetCount];
		annualisedDividend = new double[assetCount];
		volumes = new long[assetCount];
		cashflowPerShare = new double[assetCount];
		totalNetQuantities=new int[assetCount];
		
	}
	
	public int getTradersRetired() {
		return tradersRetired;
	}
	public void setTradersRetired(int tradersRetired) {
		this.tradersRetired = tradersRetired;
	}
	
	public double getCash() {
		return cash;
	}
	public void setCash(double cash) {
		this.cash = cash;
	}
	public int getTradersBlown() {
		return tradersBlown;
	}
	public void setTradersBlown(int tradersBlown) {
		this.tradersBlown = tradersBlown;
	}
	public double getAnnualisedDividend(int assetReference) {
		return annualisedDividend[assetReference];
	}
	public void setAnnualisedDividend(int assetRef,double fairPrice) {
		this.annualisedDividend[assetRef] = fairPrice;
	}
	
	public double[] getMarketPrices(){
		return marketPrices;
	}
	public double getMarketPrice(int assetReference) {
		return marketPrices[assetReference];
	}
	public void setMarketPrice(int assetReference, double marketPrice) {
		this.marketPrices[assetReference] = marketPrice;
	}
	public long getVolume(int assetReference) {
		return volumes[assetReference];
	}
	
	/**
	 * NOTE: this is volume measured as a number of shares rather than
	 * a market value. the definition using market value is used in the
	 * market information class.
	 * @return
	 */
	public long[] getVolume(){
		return volumes;
	}
	public void setVolume(int assetReference, long volume) {
		this.volumes[assetReference] = volume;
	}
	public double getCashflowPerShare(int assetReference) {
		return cashflowPerShare[assetReference];
	}
	public double[] getCashflowPerShare(){
		return cashflowPerShare;
	}
	public void setCashflowPerShare(int assetRef,double cashflowPerShare) {
		this.cashflowPerShare[assetRef] = cashflowPerShare;
	}
	
	public int[] getTotalNetQuantities() {
		return totalNetQuantities;
	}
	
	public void setTradeClassFigures(Map<Trader, Position> positions,double[] marketPrices) {
		totalWorth = 0;
		classPositions = new HashMap<Class, Position>();
		Iterator<Trader> keys = positions.keySet().iterator();
		
		int traderCount=0;
		while(keys.hasNext()){
			traderCount++;
			Trader trader =keys.next();
			Position pos = positions.get(trader);
			
			double worth=pos.getWorth(marketPrices);
			
			Position classPosition = classPositions.get(trader.getClass());
			if(classPosition==null){
				classPosition=new Position(0);
				classPositions.put(trader.getClass(), classPosition);
			}
			totalWorth +=worth;
			for(int i=0;i<marketPrices.length;i++){
				totalNetQuantities[i] += pos.getExecutedQuantity(i);
			}
			
			cash+=pos.getCash();
			
			classPosition.addPosition(pos);
		}
		System.out.println("Traders: "+traderCount);
		
	}
	public Map<Class, Position> getClassPositions() {
		return classPositions;
	}
	
	public double getTotalWorth() {
		return totalWorth;
	}
	
}
